package com.lims;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * @author limaosheng
 * @create 2023-12-06 14:25
 * @desc
 */
public class 生产与灰度配置值比对工具 {

    /**
     * 使用方式：导出全部配置，灰度以及生产的
     * 在随便空白项目的resources下建四个目录：gray（灰度）、prod（生产)
     * 将导出的配置解压复制到对应目录下
     */
    public static void main(String[] args) {
        //不同值的配置
        StringBuffer notEqual = new StringBuffer();
        //生产未包含的灰度配置
        StringBuffer grayHas = new StringBuffer();
        //灰度未包含的生产配置
        StringBuffer prodHas = new StringBuffer();
        //生产与灰度配置比对
        check(notEqual,grayHas,prodHas);

        System.out.println();
        System.out.println("--------------------分割线---------------------------");
        System.out.println("不同值的配置总结果：");
        System.out.println(notEqual);
        System.out.println("--------------------分割线---------------------------");
        System.out.println("生产未包含的灰度配置总结果：");
        System.out.println(grayHas);
        System.out.println("--------------------分割线---------------------------");
        System.out.println("灰度未包含的生产配置总结果：");
        System.out.println(prodHas);
    }

    private static void check(StringBuffer notEqual, StringBuffer grayHas, StringBuffer prodHas) {
        notEqual.append("不同值配置对比结果开始-------------------\n");
        grayHas.append("生产未包含的灰度配置对比结果开始-------------------\n");
        prodHas.append("灰度未包含的生产配置对比结果开始-------------------\n");
        //根据环境获取生产的所有文件名称
        List<String> list = getNameList();
        for(String s : list){
            TreeMap<String,String> grayMap = get("\\gray\\" + s);
            TreeMap<String,String> prodMap = get("\\prod\\" + s);

            StringBuffer notEqualOne = new StringBuffer();
            StringBuffer grayHasOne  = new StringBuffer();
            StringBuffer prodHasOne  = new StringBuffer();
            for(String o : grayMap.keySet()){
                if(prodMap.keySet().contains(o)){
                    if(!grayMap.get(o).equals(prodMap.get(o))){
                        notEqualOne.append("生产与灰度配置值不一致：" + o + ",灰度为：" + grayMap.get(o) + ",生产为："  + prodMap.get(o)  + "\n");
                    }
                }else {
                    grayHasOne.append("生产未包含的灰度配置：" + o + ":" + grayMap.get(o) + "\n");
                }
            }
            for(String o : prodMap.keySet()){
                if(!grayMap.keySet().contains(o)){
                    prodHasOne.append("灰度未包含的生产配置：" + o + ":" + prodMap.get(o) + "\n");
                }
            }
            if(notEqualOne.length() != 0){
                notEqual.append(s + "不同值文件开始比对-------------------\n");
                notEqual.append(notEqualOne);
                notEqual.append(s + "不同值文件结束比对-------------------\n");
                notEqual.append("\n");
            }
            if(grayHasOne.length() != 0){
                grayHas.append(s + "生产未包含的灰度配置文件开始比对-------------------\n");
                grayHas.append(grayHasOne);
                grayHas.append(s + "生产未包含的灰度配置文件结束比对-------------------\n");
                grayHas.append("\n");
            }
            if(prodHasOne.length() != 0){
                prodHas.append(s + "灰度未包含的生产配置文件开始比对-------------------\n");
                prodHas.append(prodHasOne);
                prodHas.append(s + "灰度未包含的生产配置文件结束比对-------------------\n");
                prodHas.append("\n");
            }
        }
        notEqual.append("不同值配置对比结果结束-------------------\n");
        grayHas.append("生产未包含的灰度配置对比结果结束-------------------\n");
        prodHas.append("灰度未包含的生产配置对比结果结束-------------------\n");
    }

    /**
     * 获取所有生产文件名
     * @return
     */
    public static List<String> getNameList(){
        List<String> list = new ArrayList<>();
        String directoryPath = System.getProperty("user.dir") + "\\src\\main\\resources\\prod";
        File directory = new File(directoryPath);
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            for (File file : files) {
                if (file.isFile()) {
                    list.add(file.getName());
                }
            }
        } else {
            System.out.println("指定的路径不存在或不是一个目录。");
        }
        return list;
    }

    public static TreeMap<String,String> get(String path) {
        TreeMap<String,String> map = new TreeMap<>();
        try {
            //1:加载配置文件
            Resource app = new ClassPathResource(path);
            //yml配置文件
            if(path.endsWith(".yml")){
                YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
                // 2:将加载的配置文件交给 YamlPropertiesFactoryBean
                yamlPropertiesFactoryBean.setResources(app);
                // 3：将yml转换成 key：val
                Properties properties = yamlPropertiesFactoryBean.getObject();
                for(Object o : properties.keySet()){
                    map.put(o.toString(),properties.get(o).toString());
                }
            //properties配置文件
            }else if(path.endsWith(".properties")){
                Properties properties = new Properties();
                try {
                    String filePath = System.getProperty("user.dir") + "\\src\\main\\resources" + path;
                    FileInputStream fileInputStream = new FileInputStream(filePath);
                    properties.load(fileInputStream);
                    fileInputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for(Object o : properties.keySet()){
                    map.put(o.toString(),properties.get(o).toString());
                }
            //普通没后缀文件，一行行读取
            }else {
                String filePath = System.getProperty("user.dir") + "\\src\\main\\resources" + path;
                File file = new File(filePath);
                Scanner scanner = new Scanner(file);
                while (scanner.hasNextLine()) {
                    String s = scanner.nextLine();
                    if(StrUtil.isNotBlank(s)){
                        if(s.contains("=")){
                            map.put(s.substring(0,s.indexOf("=")), s.substring(s.indexOf("=") + 1));
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("文件读取不到：" + path);
        }
        return map;
    }
}
